package com.imooc.controller;

import com.imooc.bo.UserBO;
import com.imooc.pojo.Users;
import com.imooc.service.UserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.JsonResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2020/6/16.
 */

@Api(value = "登录注册",tags = "相关登录注册的接口")
@RestController
@RequestMapping("passport")
public class PassportController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public JsonResult getStu(@RequestParam String username){
        if (StringUtils.isBlank(username)){
            return JsonResult.errorMsg("用户名不能为空");
        }

        boolean isExits = userService.queryUserNameIsExist(username);
        if (isExits){
            return JsonResult.errorMsg("用户名已经存在");
        }
        return JsonResult.ok();
    }

    @ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST")
    @PostMapping("/regist")
    public JsonResult regist(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response){
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPassword = userBO.getConfirmPassword();
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password) ||
                StringUtils.isBlank(confirmPassword)){
            return JsonResult.errorMsg("用户名或者密码不能为空");
        }

        boolean isExits = userService.queryUserNameIsExist(username);
        if (isExits){
            return JsonResult.errorMsg("用户名已经存在");
        }

        if (password.length() < 6){
            return JsonResult.errorMsg("密码长度不能小于6位");
        }

        if (!password.equals(confirmPassword)){
            return JsonResult.errorMsg("两次密码不一致");
        }

        Users userResult = userService.createUser(userBO);
        userResult = sentNullProperty(userResult);
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(userResult),true);
        return JsonResult.ok(userResult);
    }

    @ApiOperation(value = "用户登录",notes = "用户登录",httpMethod = "POST")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) throws Exception{
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password)){
            return JsonResult.errorMsg("用户名或者密码不能为空");
        }
       Users userResult =  userService.queryUserForLogin(username, MD5Utils.getMD5Str(password));
        if (null == userResult){
            return  JsonResult.errorMsg("用户不存在");
        }

        userResult = sentNullProperty(userResult);
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(userResult),true);
        return JsonResult.ok(userResult);
    }

    private Users sentNullProperty(Users resultUsers){
        resultUsers.setMobile(null);
        resultUsers.setMobile(null);
        resultUsers.setEmail(null);
        resultUsers.setCreatedTime(null);
        resultUsers.setUpdatedTime(null);
        resultUsers.setBirthday(null);
        return resultUsers;
    }

    @ApiOperation(value = "用户退出登录",notes = "用户退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public JsonResult logout(@RequestBody String userId, HttpServletRequest request, HttpServletResponse response)  {
        CookieUtils.deleteCookie(request,response,"user");

        // TODO 用于退出登录，需要清空购物车
        // TODO 分布式会话中需要清除出用户数据
        return JsonResult.ok();
    }

}
